<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta th:name="_csrf" th:content="${_csrf.token}"/>
    <!-- 默认的header name是X-CSRF-TOKEN -->
    <meta th:name="_csrf_header" th:content="${_csrf.headerName}"/>
    <title>新增黑名单</title>
    <script th:src="@{/assets/js/jquery.min.js}"></script>
    <link rel="stylesheet" th:href="@{/component/pear/css/pear.css}" />
</head>

<style>
    .ip_input {
        width: 40px;
        height: 20px;
        font: normal 16px 宋体;
        color: #000000;
        border: 0px;
        text-align: center;
        position: relative;
        top: 1px;
    }

    .all_input {
        width: 210px;
        height: 30px;
        font: normal 8px 宋体;
        color: #000000;
        border: 1px solid #000000;
        text-align: center;
    }

    #i1 {
        width: 260px;
        height: 30px;
        float: left;
    }
</style>
<body>
<form id="cform" class="form-inline" method="post" href="#">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">黑名单IP</label>
                    <div class="layui-input-block">

                        <div id="i1" class=all_input>
                            <input id="s1" name=ip1 class=ip_input maxlength=3
                                   onkeydown=keyDownEvent(this)
                                   onkeyup=keyUpEvent(ip1,ip1,ip2)><span
                                style="font-size: 16px">.</span>
                            <input id="s2" name=ip2 class=ip_input maxlength=3
                                   onkeydown=keyDownEvent(this)
                                   onkeyup=keyUpEvent(ip1,ip2,ip3)><span
                                style="font-size: 16px">.</span>
                            <input id="s3" name=ip3 class=ip_input maxlength=3
                                   onkeydown=keyDownEvent(this)
                                   onkeyup=keyUpEvent(ip2,ip3,ip4)><span
                                style="font-size: 16px">.</span>
                            <input id="s4" name=ip4 class=ip_input maxlength=3
                                   onkeydown=keyDownEvent(this)
                                   onkeyup=keyUpEventForIp4(ip3,ip4)>
                        </div>


                    </div>
                </div>


            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>

<script th:src="@{/component/layui/layui.js}"></script>
<script th:src="@{/component/pear/pear.js}"></script>
<script>
    layui.use(['form','jquery'],function(){
        let form = layui.form;
        let $ = layui.jquery;
        let jq = $.noConflict();
        // 获取<meta>标签中封装的_csrf信息 ,否则会请求403
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        var headers = {"X-CSRF-TOKEN": token}
        form.on('submit(user-save)', function(data){

            $.ajax({
                url:'/pear/add/BlackList',
                data: jq('#cform').serialize(),
                dataType:'json',
                //一定不能写contentType: xxx json ,不然响应结果就会输出到页面上，success回调函数就接收不到
                type:'post',
                headers: headers,
                success:function(result){
                    if(result.success){
                        layer.msg(result.msg,{icon:1,time:1000},function(){
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    }else{
                        layer.msg(result.msg,{icon:2,time:1000});
                    }
                }
            })
            return false;
        });
    })


    function getPos(obj) {
        obj.focus();
        var workRange = document.selection.createRange();
        obj.select();
        var allRange = document.selection.createRange();
        workRange.setEndPoint("StartToStart", allRange);
        var len = workRange.text.length;
        workRange.collapse(false);
        workRange.select();
        return len;
    }


    function setCursor(obj, num) {
        range = obj.createTextRange();
        range.collapse(true);
        range.moveStart('character', num);
        range.select();
    }

    function keyDownEvent(obj) {
        code = event.keyCode;
        if (!((code >= 48 && code <= 57) || (code >= 96 && code <= 105) || code == 190 || code == 110 || code == 13 || code == 9 || code == 39 || code == 8 || code == 46 || code == 99 || code == 37))
            event.returnValue = false;
        if (code == 13)
            event.keyCode = 9;
        if (code == 110 || code == 190)
            if (obj.value)
                event.keyCode = 9;
            else
                event.returnValue = false;
    }

    function keyUpEvent(obj0, obj1, obj2) {
        if (obj1.value > 255) {
            alert("填写范围必须在 0 - 255间");
            obj1.value = obj1.value.substring(0, obj1.value.length - 1);
            return;
        }
        code = event.keyCode

        if (obj1.value.length >= 3 && code != 37 && code != 39 && code != 16 && code != 9 && code != 13)
            obj2.focus();

        if (code == 32)
            obj2.focus();

        if (code == 8 && obj1.value.length == 0) {
            obj0.focus();
            setCursor(obj0, obj0.value.length);
        }

        if (code == 37 && (getPos(obj1) == 0)) {
            obj0.focus();
            setCursor(obj0, obj0.value.length);
        }
        if (code == 39 && (getPos(obj1) == obj1.value.length)) {
            obj2.focus();
        }
    }

    function keyUpEventForIp4(obj0, obj) {
        if (obj.value > 255) {
            alert("填写范围必须在 0 - 255间");
            obj.value = obj.value.substring(0, obj.value.length - 1);
            return;
        }
        if (code == 8 && obj.value.length == 0) {
            obj0.focus();
            setCursor(obj0, obj0.value.length);
        }
        if (code == 37 && (getPos(obj) == 0)) {
            obj0.focus();
            setCursor(obj0, obj0.value.length);
        }

    }

    function getipvalue(obj1, obj2, obj3, obj4, obj) {
        obj.value = obj1.value + "." + obj2.value + "." + obj3.value + "." + obj4.value;
        alert(obj.value);
    }

</script>
</body>
</html>